<?php
include_once(realpath(dirname(__FILE__))."/../configs/sms_config.php");
/**
 * 评论提示相关方法
 *
 */
class sms_util_comment{
	static $comm = '';
	static $data = '';
	
	static function init(){
		if(!self::$comm){
			self::$comm =  Model::factory('sms_model_comment');
			self::$data = Model::factory('sms_model_data');
		}
	}
	
	
	/**
	 * 情况未评论及留言数
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type
	 * @return unknown
	 */
	static function setReadedComment($uid,$type){
		self::init();
		$unReadNum = self::$data->queryUnReadSms($uid);
		if($unReadNum[$_SERVER['config']['newsms_key'][$type]]>0){
			return self::$data->updateSmsCount($uid,$type,0);
		}
		return;
	}
	/**
	 * 更新未读消息数
	 *
	 * @param unknown_type $uid  //发评论的UID	
	 * @param unknown_type  tid  //评论id
	 * @param unknown_type $control  control
	 * @param unknown_type $action	action
	 * @param unknown_type $rid	 	//资源id
	 * @param unknown_type $recom_uid	 回复给谁
	 */
	static function setUnReadComment($uid,$para = array('thread_id'=>'','res_uid'=>'','recom_uid'=>''),$ismessage=false){
		self::init();
		if(!$para['thread_id']) return false;
		/*$resid = comment_util_comment::makeTheCode($para['app'],$para['control'],$para['action'],$para['rid']);*/
		$resid = $para['thread_id'];
		$notice_user =  array();
		$filter = array($uid);
		$res_uid = $para['res_uid'];
		$num = 1;
		$_thread = sms_config::MSG_COMMENT_CNT;
		$_postType = sms_config::MSG_RECOM_CNT; 
		if($ismessage){
			$_thread = sms_config::MSG_MESSAGE_CNT;
			$_postType = sms_config::MSG_REMSG_CNT; 
		}
		if (!$res_uid) {
			$res_uid = self::$comm->getResourceOwner($resid);//获取资源所有人id
		}
		if($res_uid!=$uid){
			/*return ;//如果是自己评论或者回复自己的资源，则不提示消息
		}else{//更新资源主任的未读评论数*/
			array_push($filter,$res_uid);//过滤发送对象，如果是自己的，或者是资源的主人，就不发送
			self::$data->updateSmsCount($res_uid,$_thread,$num);//更新资源主人的未读评论数
		}
		//获取本资源的所有参与评论的人
		if($users = self::$comm->getCommentUser($resid)){
			// 去掉自己。自己发信息不会给自己提醒
			if($users = array_diff($users,$filter)){
				if($users=array_unique($users)){
					$notice_user = $users;
				}
				else return ;
			}
			else return ;
		}
		else return ;
		//$notice_user = $users;
		if($para['recom_uid']){//如果指定了回复给谁，则只给指定的人加提示
			$notice_user = array();
			foreach ($users as $list) {
				if ( $list==$para['recom_uid']) {
					array_push($notice_user,$para['recom_uid']);
					break;
				}
			}
		}
		//更新未读评论回复数
		if($notice_user){
			foreach ($notice_user as $v){
				self::$data->updateSmsCount($v,$_postType,$num);
			}
		}
		return ;
	}
	
	/**
	 * 添加评论记录信息
	 *
	 * @param unknown_type $args
	 * @param unknown_type $isComment
	 * @return unknown
	 */
	public static function upCommentLog($args = array('thread_id'=>'','uid'=>'','app'=>'','control'=>'','act'=>'','rid'=>'','res_id'=>'','authorid'=>''),$isComment=1){
		self::init();//$args = array('thread_id'=>'','uid'=>'','app'=>'','control'=>'','act'=>'','rid'=>'','authorid'=>''
//		if(!( $args['thread_id'] && $args['uid'] && $args['app'] && $args['control'] && $args['act'] && $args['rid'] && $args['authorid'])) return false;
		return self::$comm->upCommentLog($args,$isComment);
	}
	
	/**
	 * 删除评论
	 *
	 * @param unknown_type $tid
	 * @param unknown_type $uid
	 * @param unknown_type $type
	 * @return unknown
	 */
	public static function delCommentLog($tid,$uid,$type=''){
		self::init();
		return self::$comm->delComment($tid,$uid,$type);
	}
	/**
	 * 多维数组排序，按某个键对应的值排序
	 *
	 * @param unknown_type $array
	 * @param unknown_type $index	排序键值
	 * @param unknown_type $order
	 * @param unknown_type $natsort
	 * @param unknown_type $case_sensitive
	 * @return unknown
	 */
	public static function sasort (&$array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE) {
		if(is_array($array) && count($array)>0) {
			foreach(array_keys($array) as $key) 
				$temp[$key]=$array[$key][$index];
			if(!$natsort) 
				($order=='asc')? asort($temp) : arsort($temp);
			else {
				($case_sensitive)? natsort($temp) : natcasesort($temp);
				if($order!='asc') 
					$temp=array_reverse($temp,TRUE);
			}
			foreach(array_keys($temp) as $key) 
				(is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
			return $sorted;
		}
		return ;
	}
	
	/**
	 * 获取被评论资源的列表名称
	 *
	 * @param unknown_type $app
	 * @param unknown_type $control
	 * @param unknown_type $action
	 * @return unknown
	 */
	public static function getResourceTypeName($app,$control,$action){
		include_once(realpath(dirname(__FILE__))."/../configs/sms_config.php");
		$name = $callback = $key = '';
		$resTypes = $_SERVER['config']['resource']['type'];
		$resName = $_SERVER['config']['resourc_name'];
		$interface = $_SERVER['config']['resource']['interface'];
		$keys = $_SERVER['config']['resource']['para'];
		reset($resTypes);
		foreach ($resTypes as $k=>$v){
			list($_app,$_control,$_action) = $v;
			if($_app==$app && $_control==$control && $_action==$action){
				$name = $resName[$k];
				$callback = $interface[$k];
				$key = $keys[$k];
			}
		}
		return array('name'=>$name,'callback'=>$callback,'key'=>$key);
	}
	
	static function getParamaters($app,$control,$action){
		$para = '';
		include_once(realpath(dirname(__FILE__))."/../configs/sms_config.php");
	}
	/**
	 * 根据接口返回接口数据
	 *
	 * @param unknown_type $obj  接口名
	 * @param unknown_type $rid   资源id
	 * @param unknown_type $control
	 * @param unknown_type $action
	 * @return unknown
	 */
	public static function getResourceInfo($obj,$rid,$control='',$action='',$uid=''){
		if(class_exists($obj)){
			if(is_string($obj)){
				$obj = Model::factory($obj);
			}
			return call_user_func_array(array($obj,'getResourceInfoById'),array($rid,$control,$action,$uid));
		}
		else return false;
	}
	
	static function getUrl($rid,$app,$control,$action){
		return Controller::_makeURL($app,$control,$action,array('id'=>''));
	}
	
	/**
	 * 获取用户信息
	 *
	 * @param unknown_type $uid	 用户uid
	 * @param unknown_type $key	 获取列表
	 * @return unknown
	 */
	static function getUserInfoByUid($uid,$key=''){
		if(!is_numeric($uid)) return false;
		$userInfo = friends_util_cache::getUserInfo($uid);
		if ($key && $userInfo) {
			if(key_exists($key,$userInfo)){
				return $userInfo[$key];
			}
		}
		return $userInfo;

	}
	
	/**
	 * 
	 * 生成资源信息不符的html
	 * @param $obj  回调函数的类名
	 * @param $rid  资源id
	 * @param $app  
	 * @param $control
	 * @param $action
	 */
	public static function echoResourceHtml($obj,$rid='',$app='',$control='',$action='',$uid=''){
		$str = '<div class="msg-content-left">
		';
		$resInfo = self::getResourceInfo($obj,$rid,$control,$action,$uid);
		$resType = self::getResourceTypeName($app,$control,$action);
		$key = $resType['key'];
		$para = array('uid'=>$uid?$uid:$resInfo['uid']);
		if ($key) {
			$para[$key]=$resInfo['rid'];
		}
		$url = Controller::_makeURL($app,$control,$action,$para);
//		var_dump($resInfo);
		if($resInfo && $resInfo['delete']==0){
       		if('album'== $app && ('photo' == $control || 'category' == $control)){//为相册
        		$str.='
        		<div class="album130">
        		<a href="'.$url.'" target="_blank">
        		<img src="'.$resInfo['img'].'"  onload="resizeImg(this,130,130);"/>
        		</a>
        		</div><!--album130-->
        		';
        	}else if('armory'== $app && ('index' == $control)){//如果是角色信息，加上样式
        		$str.='
        			<div class="msg-content-armory">
        			';
        		$str.='    <a href="'.$url.'" target="_blank"><script>comments.breakWord("'.$resInfo['title'].'",28);</script></a>
        		</div>';
        	}
        	else {
        		$str.='    <a href="'.$url.'" target="_blank"><script>comments.breakWord("'.$resInfo['title'].'",28);</script></a>
        		';
        	}
		}
		else{
			$str.=' 该 '.$resType['name'].' 被删除！';
		}
        $str.='</div> 
        ';
        echo  $str;
	}
	
	static function getSmsCategory($uid,$app,$control,$action,$type=0){
		self::init();
		if('index'==$control && 'index'==$action){
//			echo $str.'</div>';
			return ;
		}
//		$header = $midstr=$footer='';
		$str = ' 		<div class="content-r">
		   ';
		
		/*$new_pm_num = CacheManager::getInstance()->get(array(
			'app'=>'sms',
			'func'=>array(
				array(self::$data, 'queryUnReadSms'), 
				array($uid)				
			)
		));*/
		$new_pm_num = self::$data->queryUnReadSms($uid);
		$new_sms_num = $new_pm_num['smsnum']?'<span class="rightsidebar-num">('.$new_pm_num['smsnum'].')</span>':'';
		$new_sys_num = $new_pm_num['sysnum']?'<span class="rightsidebar-num">('.$new_pm_num['sysnum'].')</span>':'';
		$new_fri_num = $new_pm_num['friendsnum']?'<span class="rightsidebar-num">('.$new_pm_num['friendsnum'].')</span>':'';
		$new_cite_num = $new_pm_num['citenum']?'<span class="rightsidebar-num">('.$new_pm_num['citenum'].')</span>':'';
		$new_comment_num = $new_pm_num['comnum']?'<span class="rightsidebar-num">('.$new_pm_num['comnum'].')</span>':'';
		$new_recom_num = $new_pm_num['recomment']?'<span class="rightsidebar-num">('.$new_pm_num['recomment'].')</span>':'';
		$new_msg_num = $new_pm_num['msgnum']?'<span class="rightsidebar-num">('.$new_pm_num['msgnum'].')</span>':'';
		$new_remsg_num = $new_pm_num['remsgnum']?'<span class="rightsidebar-num">('.$new_pm_num['remsgnum'].')</span>':'';
		$new_dkp_num = $new_pm_num['dkpnum']?'<span class="rightsidebar-num">('.$new_pm_num['dkpnum'].')</span>':'';
		$new_cartoon_num = $new_pm_num['cartoon']?'<span class="rightsidebar-num">('.$new_pm_num['cartoon'].')</span>':'';
		$new_anim_num = $new_pm_num['anim']?'<span class="rightsidebar-num">('.$new_pm_num['anim'].')</span>':'';
		$new_fiction_num = $new_pm_num['fiction']?'<span class="rightsidebar-num">('.$new_pm_num['fiction'].')</span>':'';
		//$url = Controller::_makeURL($app,$control,$action);
		if($type)
			$url.='/type/'.$type;
		switch ($control){
			case 'index':
				$title = '短信';
				$str.='				<div class="rightsidebar">
		                 <div class="rightsidebar-header">'.$title.'</div>
				    <div class="rightsidebar-content">
				       <ul class="rightsidebar-navul"><!--rightsidebar-current-nav-->
				          <li'.('view_inbox'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.index.view_inbox">收件箱</a>'.$new_sms_num.'</li>
						  <li'.('view_outbox'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.index.view_outbox">发件箱</a></li>
						  <li'.('view_send_form'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.index.view_send_form">写短信</a></li>
						</ul>
					 </div>
					</div><!--rightsidebar-content-->';
				break;
			case 'system':
				$title = '系统通知';
				$str.='       <div class="rightsidebar">
		      <div class="rightsidebar-header">'.$title.$new_sys_num.'</div>
			 <div class="rightsidebar-content">
			   <ul class="rightsidebar-navul"><!--rightsidebar-current-nav-->
			    <li'.(!$type?' class="rightsidebar-current-nav"':'').'><a href="/~sms.system.view_inbox">全部</a>'.$new_sys_num.'</li>
			   	<li'.(5==$type?' class="rightsidebar-current-nav"':'').'><a href="/~sms.system.view_inbox/type/5">好友请求</a>'.$new_fri_num.'</li>
			   	<li'.(8==$type?' class="rightsidebar-current-nav"':'').'><a href="/~sms.system.view_inbox/type/8">转帖</a>'.$new_cite_num.'</li>
			   </ul>
			  </div>
			 </div><!--rightsidebar-content-->';	
				break;
			case 'comment':
				$title = '评论';
				$str.='        <div class="rightsidebar">
		      <div class="rightsidebar-header">'.$title.'</div>
				 <div class="rightsidebar-content">
                       <ul class="rightsidebar-navul">
                          <li'.('comment_list'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.comment.comment_list">别人对我的评论'.$new_comment_num.'</a></li>
                          <li'.('mycomment_list'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.comment.mycomment_list">我对别人的评论'.$new_recom_num.'</a></li>
                          <li'.('message_list'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.comment.message_list">别人对我的留言'.$new_msg_num.'</a></li>
                          <li'.('mymessage_list'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.comment.mymessage_list">我对别人的留言'.$new_remsg_num.'</a></li>
      				   </ul>
      			</div>
      			</div><!--rightsidebar-content-->';
				break;
			case 'dkp':
			case 'fiction':
			case 'cartoon':
				$title = '我的订阅';
				$str.='        <div class="rightsidebar">
		      <div class="rightsidebar-header">'.$title.'</div>
				 <div class="rightsidebar-content">
                       <ul class="rightsidebar-navul">
                          <li'.('dkpListUid'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.dkp.dkpListUid">我订阅的DKP'.$new_comment_num.'</a></li>
                          <li'.(('cartoonListUid'==$action && 'cartoon'==$control)?' class="rightsidebar-current-nav"':'').'><a href="/~sms.cartoon.cartoonListUid">我订阅的漫画'.$new_cartoon_num.'</a></li>
                          <li'.(('fictionListUid'==$action && 'fiction'==$control)?' class="rightsidebar-current-nav"':'').'><a href="/~sms.fiction.fictionListUid">我订阅的小说'.$new_fiction_num.'</a></li>
                          <!--<li'.('mymessage_list'==$action?' class="rightsidebar-current-nav"':'').'><a href="/~sms.comment.mymessage_list">我订阅的动画'.$new_remsg_num.'</a></li>-->
      				   </ul>
      			</div>
      			</div><!--rightsidebar-content-->';
				break;
			
			default:
//				$str.='</div>';
				break;				
		}
		echo $str.'</div>';
	}
}
?>